<?php

defined('WEBWI_ROOT_PATH') or die('Restricted access');

class WebwiDatabaseFactory
{

    function WebwiDatabaseFactory()
    {
    }

    public static function &getDatabaseConnection()
    {
        static $instance;
        if (!isset($instance)) {
            if (file_exists($file = WEBWI_ROOT_PATH . '/system/classes/'.WEBWI_DB_TYPE.'database.php')) {
                require_once $file;
				
				$class = 'Webwi' . ucfirst(WEBWI_DB_TYPE) . 'DatabaseSafe';
                $instance = new $class();
                //$instance->setLogger(WebwiLogger::getInstance());
                $instance->setPrefix(WEBWI_DB_PREFIX);
                if (!$instance->connect()) {
                    trigger_error('notrace:Unable to connect to database', E_USER_ERROR);
                }
            } else {
                trigger_error('notrace:Failed to load database of type: '.WEBWI_DB_TYPE.' in file: '.__FILE__.' at line '.__LINE__, E_USER_WARNING);
            }
        }
        return $instance;
    }


    function &getDatabase()
    {
        static $database;
        if (!isset($database)) {
            if (file_exists($file = WEBWI_ROOT_PATH . '/system/classes/'.WEBWI_DB_TYPE.'database.php')) {
                include_once $file; 
                unset($database);
                $database = new $class();
            } else {
                trigger_error('notrace:Failed to load database of type: '.WEBWI_DB_TYPE.' in file: '.__FILE__.' at line '.__LINE__, E_USER_WARNING);
            }
        }
        return $database;
    }
}
?>